package com.qf.contract.controller;

import com.qf.common.base.exception.ControllerException;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.RespResult;
import com.qf.common.base.result.ResultCode;
import com.qf.contract.entity.FContractRecycleBin;
import com.qf.contract.qo.AddContractPlus;
import com.qf.contract.qo.FContractDetailsQo;
import com.qf.contract.qo.FContractQo;
import com.qf.contract.qo.SigningUnitQo;
import com.qf.contract.service.ContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;

@Api(tags = "合同登记")
@RestController
@RequestMapping("/contract")
public class ContractController {
    @Resource
    private ContractService contractService;

    @ApiOperation("新增合同")
    @PutMapping("/addcontract")
    @ApiResponses({@ApiResponse(code = 200, message = "成功")})
    public RespResult addContract(FContractQo fContractQo) {
        //设置默认时间
        try {
            fContractQo.setContractSigningDate(new Date());
            fContractQo.setContractStopDate(new Date(fContractQo.getContractSigningDate().getTime() + 1));
            return RespResult.success(contractService.addfContract(fContractQo));
        } catch (ServiceException e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        } catch (Exception e) {
            throw new ControllerException(ResultCode.SYS_ERROR);
        }
    }

    @ApiOperation("新增签订单位")
    @PutMapping("/addfcontractdetails")
    @ApiResponses({@ApiResponse(code = 200, message = "成功")})
    public RespResult addfContractDetails(FContractDetailsQo fContractDetails) {
        try {
            return RespResult.success(contractService.addfContractDetails(fContractDetails));
        } catch (ServiceException e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        } catch (Exception e) {
            throw new ControllerException(ResultCode.SYS_ERROR);
        }
    }

    @ApiOperation("新增合同明细")
    @PutMapping("/signingunit")
    @ApiResponses({@ApiResponse(code = 200, message = "成功")})
    public RespResult addContract(SigningUnitQo signingUnitQo) {
        try {
            return RespResult.success(contractService.addsigningUnit(signingUnitQo));
        } catch (ServiceException e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        } catch (Exception e) {
            throw new ControllerException(ResultCode.SYS_ERROR);
        }
    }

    @ApiOperation("合同登记")
    @PutMapping("/addcontractplus")
    @ApiResponses({@ApiResponse(code = 200, message = "成功")})
    public RespResult addcontractplus(AddContractPlus addContractPlus) {
        try {
            FContractQo fContractQo = addContractPlus.getFContractQo();
            SigningUnitQo signingUnitQo = addContractPlus.getSigningUnitQo();
            FContractDetailsQo fContractDetailsQo = addContractPlus.getFContractDetailsQo();
            //设置默认时间
            fContractQo.setContractSigningDate(new Date());
            fContractQo.setContractStopDate(new Date(fContractQo.getContractSigningDate().getTime() + 1));
            return RespResult.success(contractService.addfContractPlus(fContractQo, signingUnitQo, fContractDetailsQo));
        } catch (ServiceException e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        } catch (Exception e) {
            throw new ControllerException(ResultCode.SYS_ERROR);
        }
    }

    @ApiOperation("合同查询")
    @GetMapping("/contractrecordquery")
    @ApiResponses({@ApiResponse(code = 200, message = "成功")})
    public RespResult contractRecordQuery(@RequestParam(defaultValue = "1") int current, @RequestParam(defaultValue = "10") int size) {
        try {
            return RespResult.success(contractService.contractRecordQuery(current, size));
        } catch (ServiceException e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        } catch (Exception e) {
            throw new ControllerException(ResultCode.SYS_ERROR);
        }
    }

    @ApiOperation("逻辑删除")
    @DeleteMapping("/delcontract")
    @ApiResponses({@ApiResponse(code = 200, message = "成功")})
    public RespResult delContract(Integer[] ids, int operatorId) {
        try {
            return RespResult.success(contractService.delContract(Arrays.asList(ids), operatorId));
        } catch (ServiceException e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        } catch (Exception e) {
            throw new ControllerException(ResultCode.SYS_ERROR);
        }
    }

    @ApiOperation("删除恢复")
    @GetMapping("/deleted_recovery")
    @ApiResponses({@ApiResponse(code = 200, message = "成功")})
    public RespResult deleted_Recovery(int operatorId) {
        try {
            FContractRecycleBin fContractRecycleBin = new FContractRecycleBin();
            fContractRecycleBin.setOperatorId(operatorId);
            return RespResult.success(contractService.deleted_Recovery(fContractRecycleBin));
        } catch (ServiceException e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        } catch (Exception e) {
            throw new ControllerException(ResultCode.SYS_ERROR);
        }
    }


}
